<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="never">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="院区" prop="yardCode">
              <el-select v-model="queryParams.yardCode" placeholder="全部" clearable>
                <el-option v-for="dict in yard_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="部门名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入部门名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="success"  plain icon="refresh" @click="updateDept"
                       v-hasPermi="['budget:friendDept:update']">更新
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table border v-loading="loading" :data="friendDeptList" @selection-change="handleSelectionChange" class="enhanced-table">
        <el-table-column label="部门编号" align="center" prop="code" />
        <el-table-column label="部门名称" align="center" prop="name" min-width="150" />
        <el-table-column label="院区" align="center" prop="yardName" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="更新时间" align="center" prop="updateTime" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 更新弹窗列表 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" stripe style="width: 750px" append-to-body>
      <el-table border :data="newDeptList">
        <el-table-column label="部门编号" align="center" prop="code" />
        <el-table-column label="部门名称" align="center" prop="name" min-width="180" show-overflow-tooltip />
        <el-table-column label="院区" align="center" prop="yardName" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="170px" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="170px" />
      </el-table>
      <template #footer>
        <div class="text-right">
          <el-button type="primary" style="margin-top: 25px" @click="confirm">确定</el-button>
        </div>
      </template>

    </el-dialog>


  </div>
</template>

<script setup name="FriendDept" lang="ts">
import { listFriendDept, listNewFriendDept } from "@/api/budget/friendDept";
import { FriendDeptQuery, FriendDeptVO } from "@/api/budget/friendDept/types";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const { yard_type } = toRefs<any>(proxy?.useDict("yard_type"));
// 列表
const friendDeptList = ref<FriendDeptVO[]>([]);
// 更新后列表
const newDeptList = ref<FriendDeptVO[]>([]);

const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const friendDeptFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ""
});


const queryParams = ref<FriendDeptQuery>({
  pageNum: 1,
  pageSize: 10,
  name: undefined,
  yardCode: undefined
});


/** 查询用友部门列表 */
const getList = async () => {
  loading.value = true;
  const res = await listFriendDept(queryParams.value);
  friendDeptList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};


/** 表单重置 */
const reset = () => {
  friendDeptFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: FriendDeptVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 更新按钮操作 */
const updateDept = () => {
  proxy?.$modal.confirm("确认更新最新用友部门数据?").then(() => {
    getNewList().then(() => {
      dialog.visible = true;
      dialog.title = "新增部门";
      proxy?.$modal.msgSuccess("更新成功");
      getList();
    });
  });
};


/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download("budget/dept/export", {
    ...queryParams.value
  }, `friendDept_${new Date().getTime()}.xlsx`);
};


const getNewList = async () => {
  loading.value = true;
  const res = await listNewFriendDept();
  newDeptList.value = res.data;
  loading.value = false;
};

const confirm = () => {
  dialog.visible = false;
};
onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>

</style>
